<template>
  <div class="login">
    <el-card class="box-card">
      <h2 class="title">小罗的测试</h2>
      <el-form 
      ref="ruleFormRef" 
      :rules="rules" 
      :model="formInline" 
      class="demo-form">
        <el-form-item label="账 号" prop="username">
          <el-input v-model="formInline.username" placeholder="请输入账号名" />
        </el-form-item>
        <el-form-item label="密 码" prop="password">
          <el-input v-model="formInline.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item class="form-btn">
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang='ts'>
import type { FormInstance, FormRules } from 'element-plus'
import { ref, reactive, getCurrentInstance } from 'vue'
import { validPwd } from "@/utils/validate"
import { useRouter } from 'vue-router';
import { useAppStore } from '@/store/app';
import {setRoutes} from '@/utils/auth'
const App = useAppStore()
const router = useRouter()

const ruleFormRef = ref<FormInstance>()
const formInline = reactive({
  username: "",
  password: ""
})
const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur',
    },
    {
      message: "请输入密码",
      trigger: "blur",
      validator: (rule, value, callback) => {
        console.log(validPwd(value))
        if (value && validPwd(value)) {
          callback();
        } else {
          callback(new Error("密码必须包含数字,字母,且不少于8位"))
        }
      }
    },
  ]
})
const onSubmit = () => {
  ruleFormRef.value.validate((valid) => {
    if (!valid) return
    localStorage.setItem('token', 'login')
    localStorage.setItem('uname', formInline.username)
    console.log("当前登录用户："+formInline.username)
    App.$patch((state)=>{
      state.uname = formInline.username
    })
    setRoutes()
    /* console.log("===============")
    const routes = router.getRoutes()
    let authRoutes = routes.filter(item=>!item.meta.hidden && item.children.length>0)
    console.log(authRoutes)
    App.$patch((state)=>{
      state.routes = authRoutes
      state.uname=formInline.username
    }) */
    router.push({
      path: '/'
    })
  })
}

const reset = ()=>{
  formInline.password = ''
  formInline.username = ""
  ruleFormRef.value.resetFields()
  ruleFormRef.value.clearValidate()
}

</script>
<style lang='scss' scoped>
.login {
  background-color: #2d3a4b;
  width:100%;
  height:100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .box-card{
    width: 520px;
    height: 285px;
    .title{
      width: 100%;
      margin-top: 20px auto;
      text-align: center;
      
    }

  }
  .demo-form{
    width: 80%;
    margin-left: 8%;
    .form-btn{
      margin-top: 20px;
      :deep(.el-form-item__content){
        justify-content:center
      }
    }
  }
  
}
</style>